#!/usr/bin/env python
import pygame
import euclid
import events.dispatcher as dispatcher

def warp( window, pos ):
    """
    Warps the position around so that it always stays inside the window
    """
    assert isinstance( window, Window )
    assert type( pos ) in ( euclid.Vector2, euclid.Vector3, euclid.Point2, euclid.Point3 )
    
    if pos.x < 0: pos.x = window.width
    elif pos.x > window.width:pos.x = 0
    if pos.y < 0: pos.y = window.height
    elif pos.y > window.height: pos.y = 0
    
    return pos

class Window(object):
    def __init__(self, width, height, name, flags = 0, depth = 0):
        self.width = width
        self.height = height
        self.name = name
        
        self.screen = pygame.display.set_mode((self.width, self.height), flags, depth)
        pygame.display.set_caption( self.name )
        
        dispatcher.instance().register( dispatcher.PyGameVideoResize, self._onResize )
        
    def __str__( self ):
        return self.name + ' ( %d x %d )' %( self.width, self.height )

    def _onResize(self, event):
        self.width = event.w
        self.height = event.h

__all__ = ['warp', 'Window']